#include "stdafx.h"

#include "Simulation.h"
#include "Graphic.h"

#include "Terrain.h"

void Graphic::Draw(Simulation& SimulationObj)                             // Here's Where We Do All The Drawing
{
	glLoadIdentity();
	glTranslatef(-1.f, -1.f, 0.f);
	glScalef(2.f / SimulationObj.TerrainObj.Size, 2.f / SimulationObj.TerrainObj.Size, 1.f);

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);         // Clear The Screen And The Depth Buffer

	glBegin(GL_POINTS);
	for (int n = 0; n < SimulationObj.TerrainObj.Size * SimulationObj.TerrainObj.Size; ++n)
	{
		glColor3ub((int)SimulationObj.TerrainObj.Solid[n] * 255, 127, (int)SimulationObj.TerrainObj.Solid[n] * 255);
		glVertex2f(n % SimulationObj.TerrainObj.Size, n / SimulationObj.TerrainObj.Size);
	}

	glVertex2f(SimulationObj.StepCount % (1 + SimulationObj.TerrainObj.Size), 20);

	glEnd();

	SwapBuffers(hDC);
}

void Graphic::Create(HWND& hScene)
{
	PIXELFORMATDESCRIPTOR pfd =
	{
		sizeof(PIXELFORMATDESCRIPTOR),
		1,
		PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
		PFD_TYPE_RGBA,
		32,
		0, 0, 0, 0, 0, 0,
		0,
		0,
		0,
		0, 0, 0, 0,
		24,
		8,
		0,
		PFD_MAIN_PLANE,
		0,
		0, 0, 0
	};
	hDC = GetDC(hScene);
	int  letWindowsChooseThisPixelFormat;
	letWindowsChooseThisPixelFormat = ChoosePixelFormat(hDC, &pfd);
	SetPixelFormat(hDC, letWindowsChooseThisPixelFormat, &pfd);
	hRC = wglCreateContext(hDC);
	wglMakeCurrent(hDC, hRC);
	//MessageBoxA(0, (char*)glGetString(GL_VERSION), "OPENGL VERSION", 0); 

	glPointSize(16);
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	glClearDepth(1.0f);

	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glTranslatef(-1.f, -1.f, 0.f);
	glScalef(2.f / 100, 2.f / 100, 1.f);
}

void Graphic::Delete()
{
	wglDeleteContext(hRC);
}
